<?php
/**
*@package WPrice.app.php
*@copyright Copyright (c) 2012-2014 Tuniu.com
*@author huxiaomin 2012-07-15 10:57:07
*@version 1.0
*/	

class WPriceApp{

	function hook($name){
		if ($name == 'index'){
			return array('cache'=>1,  //是否打开文件缓存
						 'cache_time'=>1800, //文件缓存时间
						 //'tpl'=>'mytpl',  //自定义tpl的文件路径
						 'ajax'=>1,//是否使用tpl模板文件
			);
		}else if ($name == 'save'){
		  return array('ajax'=>1);
		}
		return array();
	}
	
	function index($request){
		return $data =array('key'=>'value');//使用模板用此方法
		return goutJson(1,array(),'',array());//不使用模板，或者使用ajax 要求返回是json的时候，使用此方法
		return TTpl::startOb('admin/ATable.descTable.html',$request,$request);//不使用模板或者ajax，要求返回模板别的模板内容，使用此方法
	}

	function show($request){
            $data = array();
                $route_date = trim($request['route_date']);
                $flag = trim($request['flag']);
                $data['route_id'] = $route_id = intval($request['route_id']);
                $data['date_url'] = '/adminweb/w-price/save?route_id='.$route_id;
                $data['url'] = '/adminweb/w-price/show?route_id='.$route_id;
                $cyz_calender = new cyz_calender_route($route_id,$route_date);
                switch ($flag)
                {
                    case 'last':
                            $data['calender'] = $cyz_calender->get_last_month();
                            break;
                    case 'next':
                            $data['calender'] = $cyz_calender->get_next_month();
                            break;
                        default:
                            $data['calender'] = $cyz_calender->get_month();
                }
                //print_r($data['calender']);
                $data['route_date'] = $cyz_calender->calender->year.'-'.$cyz_calender->calender->month;
                /**
                 * 显示线路
                 */
               $jsy_route = new TTable('jsy_route');
               $route = $jsy_route->get('*',array(array('id',$route_id)));
               $data['route'] = $route[0];
               return $data;	
	
	}
	
	function save($request){
	   $date = trim($request['route_date']);
            $flag = trim($request['flag']);
            $route_id = intval($request['route_id']);
            
            switch ($flag)
            {
                case 'save':
                    if (!$date)
                    {
                        $date = date('Y-m-d');
                    }
                    if (!$route_id)
                    {
                        return 'route_id error';
                        break;
                    }
                    
                    $this->date = $date;
                    $this->date = date('Y-m',strtotime($this->date));
                    $this->route_id = $route_id;
                    
                    foreach ($_POST as $key=>$value)
                    {
                        if (preg_match('/^[0-9]+$/',$key))
                        {
                            $value = explode(':',$value);
                            $this->update($value[0],$value[1],$value[2],$value[3],$key,$value[5]);
                        }
                    }
                    return 'ok';
                    break;
            }
            exit;
	   
	}
	
	private function update($price,$cost,$child_price,$child_cost,$days,$status){
	        $status = 0;
            if (trim($price) ==='' && trim($price) ==='')
            {
                $status = 1;
            }
            
            $price          = intval($price);
            $cost           = intval($cost);
            $child_price    = intval($child_price);
            $child_cost     = intval($child_cost);
            
            $days = intval($days);
            $days = str_pad($days,2,'0',STR_PAD_LEFT);
            $date = $this->date.'-'.$days;
            
            $jsy_route_date = new TTable('jsy_route_date');
            $fields = '*';
            $where = array(
                    array('route_id', $this->route_id),
                    array('route_date', $date),
            );
            $query = $jsy_route_date->get($fields, $where);
            $query = $query[0];

            $row = array(
                'route_id'=>$this->route_id,
                'route_date'=>$date,
                'price'=>$price,
                'cost' => $cost,
                'child_price'=>$child_price,
                'child_cost'=>$child_cost,
                'update_time'=>date('Y-m-d H:i:s'),
                'status'=>$status,
                'id'=>$query['id'],
            );
            if ($query)
            {
                if ($price===$query['price'] && $cost === $query['cost'] 
                    && $child_price===$query['child_price'] && $child_cost === $query['child_cost'] && $status==0 )return true;
                $return = $jsy_route_date->update($row);
                
            }else if(!$status){
            	unset($row['status']);
                $return = $jsy_route_date->insert($row);
            }
            return $return;
	}
}
	    
	    